package com.zntech.app.common.shell

import android.os.Parcel
import android.os.Parcelable
import androidx.annotation.Keep

@Keep
data class ShellResult(
    val cmd: String?,
    val exitCode: Int,
    val out: String?,
    val err: String?,
): Parcelable {
    fun isSuccessful() = (exitCode == 0)

    override fun toString(): String {
        return "Command: $cmd\nExit code: $exitCode\nOut:\n$out\n=============\nErr:\n$err"
    }

    constructor(parcel: Parcel) : this(
        parcel.readString(),
        parcel.readInt(),
        parcel.readString(),
        parcel.readString()
    )

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(cmd)
        parcel.writeInt(exitCode)
        parcel.writeString(out)
        parcel.writeString(err)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<ShellResult> {
        override fun createFromParcel(parcel: Parcel): ShellResult {
            return ShellResult(parcel)
        }

        override fun newArray(size: Int): Array<ShellResult?> {
            return arrayOfNulls(size)
        }
    }
}
